﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using webnew.Common;
using webnew.BLL;
using webnew.Model;
using System.Web.Services;
namespace webnew.Systems
{
    public partial class S_staffEdit : WebPageBase.EditBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            _ListSrc = "S_staffList.aspx";
            _ListQueryString = "CompId,DeptId,TrueName,TitleName,Status,RecordCount,CurrentPageIndex";
            //UcCompany2.SelectedIndexChanged += UcCompany2_SelectedIndexChange;
            UcCompany1.SelectedIndexChanged += UcCompany1_SelectedIndexChange;
            UcCompany1.AutoPostBack = true;
           // if (_RecordId == null) txtStaffNO.Text = new BLL.Sys_Staff().GetNewStaffNO();
            //UcCompany2.AutoPostBack = true;
            if (!IsPostBack)
            {
                
                SetTitle(true, "后台管理 >> 员工管理");
                //绑定OA单位列表,初始化部门列表
                UcCompany1.show(true, "ALL");
                ////绑定HR单位列表,初始化部门列表
                //UcCompany2.show(true, "HR");
                //初始化OA部门和HR部门             
                ddlOADept.Items.Insert(0, new ListItem("--请选择--", "0"));
               // ddlHRDept.Items.Insert(0, new ListItem("--请选择--", "0"));
                
                //加载职称数据
                UcTitleCheckBox1.ShowCheckBox();
               
            }
        }

        private void UcCompany1_SelectedIndexChange(object sender, EventArgs e) 
        {
            webnew.Common.CommonHelper.DataDdlBind(ddlOADept, GetDeptList(UcCompany1.SelectedValue), "DeptName", "id");
            ddlOADept.Items.Insert(0, new ListItem("--请选择--", "0"));
           
        }
        //private void UcCompany2_SelectedIndexChange(object sender, EventArgs e)
        //{
        //    webnew.Common.CommonHelper.DataDdlBind(ddlHRDept, GetDeptList(UcCompany2.SelectedValue), "DeptName", "id");
        //    ddlHRDept.Items.Insert(0, new ListItem("--请选择--", "0"));  
        //}

        /// <summary>
        /// 通过单位ID获取部门列表
        /// </summary>
        //[WebMethod]
        public  List<Model.Sys_DeptInfo> GetDeptList(string CompID)
        {
            return new BLL.Sys_Dept().GetDeptListByComp(CompID);
        }

        /// <summary>
        /// 重写基类，显示数据方法
        /// </summary>
        protected override void ShowData()
        {
            Model.Sys_StaffInfo model = new BLL.Sys_Staff().GetModel(_RecordId);
            txtStaffNO.Text = model.StaffNO;
            txtTrueName.Text = model.TrueName.ToString();
            rblSex.SelectedValue = model.Sex.ToString();
            UcCompany1.SelectedValue = model.OACompId.ToString();
            //UcCompany2.SelectedValue = model.HRCompId.ToString();
            UcCompany1_SelectedIndexChange(null, null);
            //UcCompany2_SelectedIndexChange(null, null);
            //webnew.Common.CommonHelper.DataDdlBind(ddlOADept, GetDeptList(UcCompany1.SelectedValue), "DeptName", "id");
            //ddlOADept.Items.Insert(0, new ListItem("--请选择--", "0"));
            //webnew.Common.CommonHelper.DataDdlBind(ddlHRDept, GetDeptList(UcCompany2.SelectedValue), "DeptName", "id");
            //ddlHRDept.Items.Insert(0, new ListItem("--请选择--", "0"));
            ddlOADept.SelectedValue = model.OADeptId.ToString();
           // ddlHRDept.SelectedValue = model.HRDeptId.ToString();
            hfStaffId.Value = model.LeaderId;
            if (model.LeaderId!=string.Empty) txtStaffName.Text = hfStaffName.Value = new BLL.Sys_Staff().GetModel(model.LeaderId).TrueName;
            UcTitleCheckBox1.ShowCheckBox(model.titleName);
            ddlisdel.SelectedValue = model.isdel.ToString();
            txtremark.Text = model.remark.ToString();
        }

        /// <summary>
        /// 重写基类，传入要验证的数据
        /// </summary>
        protected override List<PageValid> _SetValidData() {
            List<PageValid> lst = new List<PageValid>();

            lst.Add(new PageValid("员工姓名", txtTrueName.Text, PageValid.validType.CommonValid, false, 25));
            lst.Add(new PageValid("请选择", Guid.Empty.ToString(), UcCompany1.SelectedValue, PageValid.validType.NoSameValid, "所属OA单位"));
            //lst.Add(new PageValid("请选择", "0", ddlOADept.SelectedValue, PageValid.validType.NoSameValid, "所属OA部门"));
           // lst.Add(new PageValid("上级领导", hfStaffId.Value, PageValid.validType.CommonValid, false, 50));
           // lst.Add(new PageValid("请选择", "0", ddlHRDept.SelectedValue, PageValid.validType.NoSameValid, "所属HR部门"));
           // lst.Add(new PageValid("请选择", Guid.Empty.ToString(), UcCompany2.SelectedValue, PageValid.validType.NoSameValid, "所属HR单位"));
            lst.Add(new PageValid("备注",txtremark.Text ,PageValid.validType.CommonValid, true, 500 ));
            if (_RecordId == null)
            {
                lst.Add(new PageValid("用户ID", "1", new BLL.Sys_Staff().ExistStaffNO(txtStaffNO.Text).ToString(), PageValid.validType.NoSameValid, "已存在，请重新输入"));
            }
            return lst;
        }

        /// <summary>
        /// 重写基类保存数据方法
        /// </summary>
        protected override int SaveData()
        {
            Model.Sys_StaffInfo model = new Model.Sys_StaffInfo();
            model.Id = _RecordId;
            model.TrueName = txtTrueName.Text;
            model.Sex =rblSex.SelectedValue;
            model.OACompId = UcCompany1.SelectedValue;
            model.OADeptId = ddlOADept.SelectedValue == "0" ? Guid.Empty.ToString() : ddlOADept.SelectedValue;
            model.LeaderId = hfStaffId.Value;
            //model.HRCompId = UcCompany2.SelectedValue;
            //model.HRDeptId = ddlHRDept.SelectedValue;
            model.titleName = UcTitleCheckBox1.GetAllCheckedValue();
            model.isdel =Convert.ToInt32(ddlisdel.SelectedValue);
            model.CreateDate = DateTime.Now;
            model.remark = txtremark.Text;

            //----------系统日志model

            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息

            if (_RecordId != null)  //修改
            {
                try
                {
                return new BLL.Sys_Staff().Modify(model);
                }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "修改", modelLog, "员工档案", "员工档案，员工名称为" + txtTrueName.Text);
                }
            }
            else                //添加
            {
                try 
                {
                    model.StaffNO = new BLL.Sys_Staff().GetNewStaffNO();
                    return new BLL.Sys_Staff().Add(model);
                }
                catch (Exception e)
                {
                    error = e.ToString();
                }
                finally
                {
                    //添加一条系统日志
                    new BLL.Sys_Log().AddSysLog(error, "添加", modelLog, "员工档案", "员工档案，员工名称为" + txtTrueName.Text);
                }
            }
            //弹出文本框成功或者失败
            if (error == "") { return 1; }       //成功
            else { return 0; }                   //失败
        }
    }
}
